package dao.impl;

import java.util.List;

import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import bean.ArticleClass;
import dao.ArticleClassDao;

public class ArticleClassDaoImpl extends HibernateDaoSupport implements ArticleClassDao {

	@Override
	public boolean addArticleClass(ArticleClass ArticleClass) {
		this.getSessionFactory().getCurrentSession().save(ArticleClass);
		return true;
	}

	@Override
	public boolean deleteArticleClass(ArticleClass ArticleClass) {
		// TODO Auto-generated method stub
		this.getSessionFactory().getCurrentSession().delete(ArticleClass);
		return true;
	}

	@Override
	public boolean modifyArticleClass(ArticleClass ArticleClass) {
		// TODO Auto-generated method stub
		this.getSessionFactory().getCurrentSession().update(ArticleClass);
		return true;
	}

	@Override
	public ArticleClass findArticleClassByTitle(ArticleClass articleClass) {
		List l=this.getSessionFactory().getCurrentSession().createCriteria(ArticleClass.class).add(Restrictions.eq("user.id", articleClass.getUser().getId())).add(Restrictions.eq("title", articleClass.getTitle())).list();
		if(l.size()==0)
			return null;
		else
			return (ArticleClass) l.get(0);
	}

	@Override
	public ArticleClass findArticleClassById(int id) {
		// TODO Auto-generated method stub
		Object o=this.getSessionFactory().getCurrentSession().load(ArticleClass.class, id);
		if(o==null)
		return null;
		else
			return (ArticleClass) o;
	}

	@Override
	public List findArticleClassesByUserid(int id) {
		//List l=this.getSessionFactory().getCurrentSession().createQuery("from ArticleClass ac where ac.user.userid='"+id+"'").list();
		List l=this.getSessionFactory().getCurrentSession().createCriteria(ArticleClass.class).add(Restrictions.eq("user.id", id)).list();
		
		return l;
	}

}
